<template>
  <div class="upload-cover" @click="showCoverSelect">
    <div class="cover-wrap">
      <!-- <img
          class="cover-image"
          ref="cover-image"
          :src="coverImage"
          > -->
          <img
          class="cover-image"
          ref="cover-image"
          :src="value"
          >
      <el-dialog
        title="选择封面"
        :visible.sync="dialogVisible"
        append-to-body
      >
        <el-tabs v-model="activeName" type="card">
          <el-tab-pane label="素材库" name="first">
            <!-- ref有两个作用
                  1、作用到普通HTML标签上可以获取DOM
                  2、作用到组件上可以获取组件
            -->
            <image-list
              :is-show="false"
              ref="image-list"
              is-show-selected
            ></image-list>
          </el-tab-pane>
          <el-tab-pane label="上传图片" name="second">
            <input
              type="file"
              ref="file"
              @change="onFileChange"
            />
            <div class="preview-image-wrap">
              <img
                class="preview-image"
                ref="preview-image"
              />
            </div>
          </el-tab-pane>
        </el-tabs>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="onCoverConfirm">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { uploadImage } from '@/api/image'
import ImageList from '@v/image/components/image-list.vue'
export default {
  name: 'UploadCover',
  data() {
    return {
      dialogVisible: false,
      previewImage: '',
      activeName: 'second'
    }
  },
  methods: {
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then((_) => {
          done()
        })
        .catch((_) => {})
    },
    // 选择封面
    showCoverSelect() {
      this.dialogVisible = true
    },
    onFileChange() {
      // 获取文件对象
      const file = this.$refs.file.files[0]
      // 图片预览

      const blob = window.URL.createObjectURL(file)
      // console.log(blob)
      this.$refs['preview-image'].src = blob
      // this.previewImage = blob

      // 解决选择相同文件不触发change事件的问题
      //  this.$refs.file.value = ''
    },
    onCoverConfirm() {
      // 如果tab 是上传图片，并且input-file有选择的文件，才执行上传图片的操作
      if (this.activeName === 'second') {
        const file = this.$refs.file.files[0]
        if (!file) {
          this.$message('请选择文件')
          return
        }
        // 执行上传文件的操作
        const fd = new FormData()
        fd.append('image', file)
        uploadImage(fd).then(res => {
          // 关闭弹出层
          this.dialogVisible = false
          // 展示上传图片
          this.$refs['cover-image'].src = res.data.data.url

          // 将图片地址发送到父组件
          // this.$emit('upload-cover', res.data.data.url)
          this.$emit('input', res.data.data.url)
        })
      } else if (this.activeName === 'first') {
        const imageList = this.$refs['image-list']
        console.log(imageList.selected)

        if (imageList.selected == null) {
          this.$message('请选择图片')
          return
        }
        // 关闭对话框
        this.dialogVisible = false
        // 修改父组件绑定数据
        this.$emit('input', imageList.images[imageList.selected].url)
      }
    }
  },
  created() {},
  mounted() {},
  components: { ImageList },
  // props: ['cover-image']
  props: ['value']
}
</script>

<style scoped lang = "less">
.cover-wrap {
  width: 150px;
  height: 120px;
  border: 1px solid #000;
  .cover-image {
    width: 100%;
    height: 120px;
  }
}
.preview-image-wrap{
  .preview-image{
     display: block;
     max-width: 100%;
     height:200px;
  }
}
</style>
